import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import '../../common/style/colors.dart';
import '../../utils/log_utils.dart';
import '../loading/loading_widget.dart';

class LoadingDialog extends StatelessWidget {
  const LoadingDialog({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), color: AppColors.dialogBg),
      width: 110,
      height: 110,
      child: const Material(
        color: AppColors.dialogBg,
        elevation: 24,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.0))),
        type: MaterialType.transparency,
        child: LoadingWidget(),
      ),
    ));
  }
}

void showLoading({CancelToken? cancelToken}) {
  if (cancelToken != null) {
    SmartDialog.showLoading(
      builder: (context) {
        return WillPopScope(
            child: const LoadingDialog(),
            onWillPop: () async {
              cancelToken.cancel("cancelled");
              LogUtils.d("取消请求");
              return true;
            });
      },
    );
    // Get.dialog(WillPopScope(
    //     child: const LoadingDialog(),
    //     onWillPop: () async {
    //       cancelToken.cancel("cancelled");
    //       LogUtils.d("取消请求");
    //       return true;
    //     }));
  } else {
    SmartDialog.showLoading(
      builder: (context) {
        return const LoadingDialog();
      },
    );
  }
}

void dismissLoading() {
  SmartDialog.dismiss(status: SmartStatus.loading);
}
